package com.sise.parkinglot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sise.parkinglot.bean.CodeMsg;
import com.sise.parkinglot.entity.AdminLogin;
import com.sise.parkinglot.mapper.AdminLoginMapper;
import com.sise.parkinglot.service.IAdminLoginService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ZJ
 */
@Service
public class AdminLoginServiceImpl extends ServiceImpl<AdminLoginMapper, AdminLogin> implements IAdminLoginService {

    @Resource
    private AdminLoginMapper adminLoginMapper;

    @Override
    public CodeMsg<List<AdminLogin>> queryAdminLoginList(Map<String, Object> map) {
        if (map.isEmpty()) {
            return new CodeMsg<List<AdminLogin>>().setCode(400).setMsg("参数缺失");
        }
        Page<AdminLogin> page = new Page<>((int) map.get("current"), (int) map.get("pageSize"));
        QueryWrapper<AdminLogin> adminLoginQueryWrapper = new QueryWrapper<>();
        if (map.get("id") != null && !map.get("id").equals("")) {
            adminLoginQueryWrapper.eq("id", map.get("id"));
        }
        if (map.get("username") != null && !map.get("username").equals("")) {
            adminLoginQueryWrapper.like("username", map.get("username"));
        }
        if (map.get("password") != null && !map.get("password").equals("")) {
            adminLoginQueryWrapper.like("password", map.get("password"));
        }
        if (map.get("msg") != null && !map.get("msg").equals("")) {
            adminLoginQueryWrapper.like("station_type", map.get("msg"));
        }
        if (map.get("loginTime") != null && !map.get("loginTime").equals("")) {
            String createTime = (String) map.get("loginTime");
            adminLoginQueryWrapper.likeRight("login_time", createTime.split(" ")[0]);
        }
        adminLoginQueryWrapper.lambda().orderByDesc(AdminLogin::getLoginTime);
        IPage<AdminLogin> adminLoginPage = adminLoginMapper.selectPage(page, adminLoginQueryWrapper);
        List<AdminLogin> adminLoginList = adminLoginPage.getRecords();
        return new CodeMsg<List<AdminLogin>>().setCode(200).setMsg(String.valueOf(adminLoginPage.getTotal())).setData(adminLoginList);
    }
}
